﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Easily
{
    public partial class frmUEcomInit : Form
    {
        GeneralIcon MyIcon;
        public frmUEcomInit(GeneralIcon icon)
        {
            InitializeComponent();
            MyIcon = icon;
            //初始化控件初值
            if (this.MyIcon.code.Save != "")  //加载已保存的控件信息
            {
                string[] tmp = this.MyIcon.code.Save.Split(';');
                txt_ip.Text = tmp[0];
                txt_port.Text = tmp[1];
                createCode();//根据配置生成代码
            }
        }
        /// <summary>
        /// 点击“确定”按钮
        /// 主要功能：根据配置生成新代码，并设置对话返回为OK
        /// </summary>
        private void Btn_confirm_Click(object sender, EventArgs e)
        {
            createCode();//根据配置生成代码
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 单击“取消”按钮
        /// 主要功能：设置返回结果为取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        #region 内部函数
        /// <summary>
        /// 生成保存于MyIcon.code中的内容
        /// </summary>
        


        private void createCode()
        {
            //添加头文件、初始化代码等
            MyIcon.code.Clear();//清空以前代码
            MyIcon.code.HeadFile = "#include \"wifi.h\""; //添加构件的头文件
            MyIcon.code.Initialization = "uart_init(UART_UE,112500)";
            MyIcon.code.InterruptEnable = "uart_enable(UART_UE)";
            MyIcon.code.Comment = "与服务器" + txt_ip.Text + ":" + txt_port.Text + "建立连接";
            MyIcon.code.InsertCode = "linkServer(\"" + txt_ip.Text + "\",\"" + txt_port.Text + "\",\"" + txt_port2.Text + "\"" +
                ",\"" + txt_SSID.Text + "\",\"" + txt_PSSWD.Text + "\");//连接服务器";
            MyIcon.code.Save = txt_ip.Text + ";" + txt_port.Text;
        }
        #endregion

        private void frmUEcomInit_Load(object sender, EventArgs e)
        {

        }

        private void txt_ip_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
